
//
//  UIButton+extention.swift
//  Swift微博
//
//  Created by 在下叶良辰 on 16/6/4.
//  Copyright © 2016年 谢浩源. All rights reserved.
//

import UIKit
extension UIButton {
    // 自定义的init方法和NSObject的init方法冲突
    // 解决方法就在init()里面加参数就可以了
    convenience init(title: String,
        titleColor: UIColor = UIColor.darkGrayColor(),
        font: CGFloat = 12,
        image: String? = nil,
        backGroudImage: String? = nil,
        target: AnyObject? = nil,
        action: String? = nil,
        event: UIControlEvents = .TouchUpInside) {
        
        self.init()
        
        self.setTitle(title, forState: .Normal)
        self.setTitleColor(titleColor, forState: .Normal)
        
        self.titleLabel?.font = UIFont.systemFontOfSize(font)
        
        if let image = image {
            
            self.setImage(UIImage(named: image), forState: .Normal)
            self.setImage(UIImage(named: "\(image)_highlighted"), forState: .Highlighted)
        }
        
        if let backGroudImage = backGroudImage {
            
            self.setBackgroundImage(UIImage(named: backGroudImage), forState: .Normal)
            self.setBackgroundImage(UIImage(named: "\(backGroudImage)_highlighted"), forState: .Highlighted)
        }
        
        // action如果直接写, 可以直接用字符串, 如果作为参数传输, 必须使用 selector
            let selector = Selector(action!)
            self.addTarget(target, action: selector, forControlEvents: event)
    }
}
